package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.User;
import com.campsys.entity.Category;
import com.campsys.service.dto.UserQueryDTO;
import com.campsys.service.dto.UserDTO;
import com.campsys.service.dto.CategoryDTO;
import com.campsys.service.UserService;
import com.campsys.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/root")
@Slf4j
public class RootController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private CategoryService categoryService;
    
    // 用户相关接口
    @PostMapping("/user/list")
    public Result getUserList(@RequestBody UserQueryDTO queryDTO) {
        Page<User> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(userService.getUserList(page, queryDTO));
    }
    
    @PostMapping("/user/add")
    public Result addUser(@RequestBody @Validated UserDTO userDTO) {
        userService.addUser(userDTO);
        return Result.ok();
    }
    
    @PostMapping("/user/update")
    public Result updateUser(@RequestBody @Validated UserDTO userDTO) {
        if (userDTO.getId() == null) {
            return Result.error("用户ID不能为空");
        }
        userService.updateUser(userDTO);
        return Result.ok();
    }
    
    @PostMapping("/user/delete/{id}")
    public Result deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.ok();
    }
    
    @PostMapping("/user/status/{id}")
    public Result updateUserStatus(@PathVariable Long id, @RequestParam Integer status) {
        if (status != 0 && status != 1) {
            return Result.error("状态值只能是0或1");
        }
        userService.updateStatus(id, status);
        return Result.ok();
    }
    
    // 分类相关接口
    @GetMapping("/category/list")
    public Result getCategoryList(@RequestParam(required = false) Integer type) {
        return Result.ok(categoryService.getCategoryList(type));
    }
    
    @PostMapping("/category/add")
    public Result addCategory(@RequestBody @Validated CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryService.addCategory(category);
        return Result.ok();
    }
    
    @PostMapping("/category/update")
    public Result updateCategory(@RequestBody @Validated CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryService.updateCategory(category);
        return Result.ok();
    }
    
    @PostMapping("/category/delete/{id}")
    public Result deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.ok();
    }
} 